package itbour.onetouchshow.utils;

import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.util.List;

/**
 * Created by Administrator on 2018/2/1.
 */

public class ThirdAppInstall {

    private static boolean isWeiXinInstall, isQQInstall, isWeiBoInstall;

    //将三个判断集中到一个 三遍遍历效率较低 弹框弹出的等待时间较长
    public static void cheackThirdPartIsInstall(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    isWeiXinInstall = true;
                }
                if (pn.equals("com.tencent.mobileqq")) {
                    isQQInstall = true;
                }
                if (pn.equals("com.sina.weibo")) {
                    isWeiBoInstall = true;
                }
            }
        }
    }

    //三个中是否有一个安装
    public static boolean isOnePartInstall() {
        return isQQInstall || isWeiBoInstall || isWeiXinInstall;
    }

    public static boolean isWeixinAvilible(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * 判断qq是否可用
     *
     * @param context
     * @return
     */
    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }


    /**
     * 检测是否安装微信
     *
     * @param context
     * @return
     */
    public static boolean isWBClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.sina.weibo")) {
                    return true;
                }
            }
        }
        return false;
    }


    public static boolean isAvailableOne(Context context) {
        boolean isAvailableOne = false;
        if (isQQClientAvailable(context)) {
            isAvailableOne = true;
        }
        if (isWBClientAvailable(context)) {
            isAvailableOne = true;
        }
        if (isWeixinAvilible(context)) {
            isAvailableOne = true;
        }
        return isAvailableOne;
    }


    /**
     * 跳转到微信
     */
    public static void startWechatApi(Context context) {
        try {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(cmp);
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {

        }
    }

    /**
     * 跳转到qq
     */
    public static void startQQApi(Context context) {
        try {
            Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.tencent.mobileqq");
            //  ComponentName cmp = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.HomeActivity");
            // intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // intent.setComponent(cmp);
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {

        }
    }


    /**
     * 跳转到微博
     */
    public static void startWBApi(Context context) {
        try {
            Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.sina.weibo");
            //ComponentName cmp = new ComponentName("com.sina.weibo", " com.sina.weibo.EditActivity");
            //intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // intent.setComponent(cmp);
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {

        }
    }

}
